import { _decorator, Button, Component, Label, Toggle } from 'cc';
import { Databus } from '../core/Databus';
import { BagManager } from '../game/BagManager';
import { RewardedVideoAd } from '../platform/ad/RewardedVideoAd';
import { updateSetting } from '../services/Setting';
import { UniformFunction } from '../platform/UniformFunction';
import { InterstitialAd } from '../platform/ad/InterstitialAd';

const { ccclass, property } = _decorator;

const databus = Databus.Instance();
@ccclass('BackPackPage')
export class BackPackPage extends Component {
  public mapInstanceName = [
    ['DefaultQiuGan', 'HighLevelQiuGan', 'LuxuryQiuGan', 'PerfectQiuGan'],
    ['DefaultTable', 'HighLevelTable', 'LuxuryTable', 'PerfectTable'],
    ['DefaultRoom', 'HighLevelRoom', 'LuxuryRoom', 'PerfectRoom'],
  ];
  public mapName = [
    ['经典球杆', '黑白球杆', '紫色球杆', '高端球杆'],
    ['经典球桌', '深蓝球桌', '红色球桌', '高端球桌'],
    ['经典房间', '高端房间', '豪华房间', '至尊房间'],
  ];
  public mapDesc = [
    ['瞄准线长0.3米', '瞄准线长0.3米(增加50%)', '瞄准线长0.5米(增加50%)', '瞄准线长0.5米(增加50%)'],
    ['经典球桌', '深蓝球桌', '红色球桌', '高端球桌'],
    ['经典房间', '高端房间', '豪华房间', '至尊房间'],
  ];
  public menuIndex = 0; // 0 球杆 1 球桌 2 房间配置 3 球
  public selectIndex = 0; // 可以选择的 名称
  public backButton: Button = null;
  public useButton: Button = null;
  public onLoad(): void {
    this.backButton = this.node.getChildByName('BackButton').getComponent(Button);
    this.useButton = this.node.getChildByName('UseButton').getComponent(Button);
    this.backButton.node.on(Button.EventType.CLICK, this.backPage, this);
    this.useButton.node.on(Button.EventType.CLICK, this.useObject, this);
  }




  public start() {
    RewardedVideoAd.Instance().create();
    InterstitialAd.Instance().show();
  }
  public update(deltaTime: number) {
    const menuIndex = this.chooseMenuIndex();
    const selectIndex = this.chooseSelectIndex();
    if (menuIndex !== this.menuIndex || this.selectIndex !== selectIndex) {
      this.menuIndex = menuIndex;
      this.selectIndex = selectIndex;
      console.log(this.mapInstanceName[this.menuIndex][this.selectIndex]);
      BagManager.Instance().showInstance(this.mapInstanceName[this.menuIndex][this.selectIndex]);
    }

    this.updateChooseFlag();
  }

  public updateChooseFlag() {
    if (this.menuIndex <= 2 ) {
      const desc = this.node.getChildByName('SelectView').getChildByName('ObjectDesc').getChildByName('Desc');
      desc.getComponent(Label).string = `${this.mapDesc[this.menuIndex][this.selectIndex]}`;
      this.useButton.node.active = true;
    }

    if (this.mapInstanceName[this.menuIndex][this.selectIndex] === databus.outlook.ganName ||
      this.mapInstanceName[this.menuIndex][this.selectIndex] === databus.outlook.tableName ||
      this.mapInstanceName[this.menuIndex][this.selectIndex] === databus.outlook.roomName
    ) {
      this.node.getChildByName('SelectView').getChildByName('ChooseFlag').getChildByName('ChoosedFlagLabel').active = true;
      this.node.getChildByName('SelectView').getChildByName('ChooseFlag').getChildByName('UnChoosedFlagLabel').active = false;
    } else {
      this.node.getChildByName('SelectView').getChildByName('ChooseFlag').getChildByName('ChoosedFlagLabel').active = false;
      this.node.getChildByName('SelectView').getChildByName('ChooseFlag').getChildByName('UnChoosedFlagLabel').active = true;
    }
  }

  public useObject() {
    RewardedVideoAd.Instance().setCallback(this, this.watchVideoSuss, this.watchVideoFail);
    RewardedVideoAd.Instance().show();
  }

  public backPage() {
    databus.sceneStack.backScene();
  }

  protected watchVideoSuss() {
    const name = this.mapInstanceName[this.menuIndex][this.selectIndex];
    databus.updateBagPack(name);
    new UniformFunction().showInfoToast('领取成功', 3000);
    updateSetting(databus.selfInfo.userId, databus.selfInfo.userId, 5, 'bag', JSON.stringify(databus.outlook));
  }
  protected watchVideoFail() {
    new UniformFunction().showInfoToast('领取失败', 3000);
  }
  private chooseMenuIndex() {
    var index = 0;
    const groupSelector = this.node.getChildByName('MenuToggleGroup');
    for (const ts of groupSelector.children) {
      const tog = ts.getComponent(Toggle);
      if (tog.isChecked) { break; }
      index += 1;
    }
    if (index >= 3) { index = 0; }
    var i=0;
    for (const tog of this.node.getChildByName('SelectToggleGroup').children) {
      tog.getComponent(Label).string = this.mapName[index][i];
      i += 1;
    }
    return index;
  }
  private chooseSelectIndex() {
    var index = 0;
    const groupSelector = this.node.getChildByName('SelectToggleGroup');
    for (const ts of groupSelector.children) {
      const tog = ts.getComponent(Toggle);
      if (tog.isChecked) return index;
      index += 1;
    }
    return index;
  }
}